package gov.jyy.operationlog.aop;

import gov.jyy.operationlog.core.OperationLogHandle;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import javax.annotation.Resource;

@Aspect
public class OperationLogAspect {

    @Resource
    private OperationLogHandle operationLogHandle;

    @Pointcut("@annotation(gov.jyy.operationlog.core.OperationLog)")
    public void operationLogPointcut() {
    }

    @Around("operationLogPointcut()")
    public Object logAfterThrowing(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result;
        try {
            result = joinPoint.proceed();
        } catch (Throwable throwable) {
            operationLogHandle.handleLog(joinPoint, throwable);
            throw throwable;
        }
        operationLogHandle.handleLog(joinPoint, null);
        return result;
    }


}
